
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.io.cif &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.cif</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.io.cif</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Wrapper classes for Cif input and output from Structures.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">textwrap</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span><span class="p">,</span> <span class="n">deque</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">getfullargspec</span> <span class="k">as</span> <span class="n">getargspec</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">groupby</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.periodic_table</span> <span class="kn">import</span> <span class="n">Element</span><span class="p">,</span> <span class="n">Specie</span><span class="p">,</span> <span class="n">get_el_sp</span><span class="p">,</span> <span class="n">DummySpecie</span>
<span class="kn">from</span> <span class="nn">monty.io</span> <span class="kn">import</span> <span class="n">zopen</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.coord</span> <span class="kn">import</span> <span class="n">in_coord_list_pbc</span><span class="p">,</span> <span class="n">find_in_coord_list_pbc</span>
<span class="kn">from</span> <span class="nn">monty.string</span> <span class="kn">import</span> <span class="n">remove_non_ascii</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.lattice</span> <span class="kn">import</span> <span class="n">Lattice</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.structure</span> <span class="kn">import</span> <span class="n">Structure</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.composition</span> <span class="kn">import</span> <span class="n">Composition</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.operations</span> <span class="kn">import</span> <span class="n">SymmOp</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.groups</span> <span class="kn">import</span> <span class="n">SpaceGroup</span><span class="p">,</span> <span class="n">SYMM_DATA</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>
<span class="kn">from</span> <span class="nn">pymatgen.electronic_structure.core</span> <span class="kn">import</span> <span class="n">Magmom</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.operations</span> <span class="kn">import</span> <span class="n">MagSymmOp</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.maggroups</span> <span class="kn">import</span> <span class="n">MagneticSpaceGroup</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong, Will Richards, Matthew Horton&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2011, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;4.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyuep@gmail.com&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;Sep 23, 2011&quot;</span>

<span class="n">sub_spgrp</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;[\s_]&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>

<span class="n">space_groups</span> <span class="o">=</span> <span class="p">{</span><span class="n">sub_spgrp</span><span class="p">(</span><span class="n">k</span><span class="p">):</span> <span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">SYMM_DATA</span><span class="p">[</span><span class="s1">&#39;space_group_encoding&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()}</span>  <span class="c1"># type: ignore</span>

<span class="n">space_groups</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">sub_spgrp</span><span class="p">(</span><span class="n">k</span><span class="p">):</span> <span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">SYMM_DATA</span><span class="p">[</span><span class="s1">&#39;space_group_encoding&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()})</span>  <span class="c1"># type: ignore</span>

<span class="n">_COD_DATA</span> <span class="o">=</span> <span class="kc">None</span>


<span class="k">def</span> <span class="nf">_get_cod_data</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">_COD_DATA</span>
    <span class="k">if</span> <span class="n">_COD_DATA</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">pymatgen</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">pymatgen</span><span class="o">.</span><span class="n">symmetry</span><span class="o">.</span><span class="n">__path__</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                               <span class="s2">&quot;symm_ops.json&quot;</span><span class="p">))</span> \
                <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="kn">import</span> <span class="nn">json</span>
            <span class="n">_COD_DATA</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">_COD_DATA</span>


<div class="viewcode-block" id="CifBlock"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifBlock">[docs]</a><span class="k">class</span> <span class="nc">CifBlock</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Object for storing cif data. All data is stored in a single dictionary.</span>
<span class="sd">    Data inside loops are stored in lists in the data dictionary, and</span>
<span class="sd">    information on which keys are grouped together are stored in the loops</span>
<span class="sd">    attribute.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">maxlen</span> <span class="o">=</span> <span class="mi">70</span>  <span class="c1"># not quite 80 so we can deal with semicolons and things</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">loops</span><span class="p">,</span> <span class="n">header</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            data: dict or OrderedDict of data to go into the cif. Values should</span>
<span class="sd">                    be convertible to string, or lists of these if the key is</span>
<span class="sd">                    in a loop</span>
<span class="sd">            loops: list of lists of keys, grouped by which loop they should</span>
<span class="sd">                    appear in</span>
<span class="sd">            header: name of the block (appears after the data_ on the first</span>
<span class="sd">                line)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loops</span> <span class="o">=</span> <span class="n">loops</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
        <span class="c1"># AJ says: CIF Block names cannot be more than 75 characters or you</span>
        <span class="c1"># get an Exception</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">header</span><span class="p">[:</span><span class="mi">74</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">loops</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">loops</span> \
               <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">data</span> \
               <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">header</span>

    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the cif string for the data block</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;data_</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)]</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
        <span class="n">written</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">written</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">loops</span><span class="p">:</span>
                <span class="c1"># search for a corresponding loop</span>
                <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
                    <span class="n">s</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_loop_to_string</span><span class="p">(</span><span class="n">l</span><span class="p">))</span>
                    <span class="n">written</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
                    <span class="k">break</span>
            <span class="k">if</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">written</span><span class="p">:</span>
                <span class="c1"># k didn&#39;t belong to a loop</span>
                <span class="n">v</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_format_field</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxlen</span><span class="p">:</span>
                    <span class="n">s</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">   </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">s</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">])</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_loop_to_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">loop</span><span class="p">):</span>
        <span class="n">s</span> <span class="o">=</span> <span class="s2">&quot;loop_&quot;</span>
        <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">loop</span><span class="p">:</span>
            <span class="n">s</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="n">l</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">loop</span><span class="p">]):</span>
            <span class="n">line</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
            <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_format_field</span><span class="p">,</span> <span class="n">fields</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;;&quot;</span><span class="p">:</span>
                    <span class="n">s</span> <span class="o">+=</span> <span class="n">line</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">val</span>
                    <span class="n">line</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
                <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxlen</span><span class="p">:</span>
                    <span class="n">line</span> <span class="o">+=</span> <span class="s2">&quot;  &quot;</span> <span class="o">+</span> <span class="n">val</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">s</span> <span class="o">+=</span> <span class="n">line</span>
                    <span class="n">line</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">  &#39;</span> <span class="o">+</span> <span class="n">val</span>
            <span class="n">s</span> <span class="o">+=</span> <span class="n">line</span>
        <span class="k">return</span> <span class="n">s</span>

    <span class="k">def</span> <span class="nf">_format_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
        <span class="n">v</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxlen</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxlen</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">;&#39;</span>
        <span class="c1"># add quotes if necessary</span>
        <span class="k">if</span> <span class="n">v</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;&quot;&quot;&#39;</span>
        <span class="k">if</span> <span class="p">(</span><span class="s2">&quot; &quot;</span> <span class="ow">in</span> <span class="n">v</span> <span class="ow">or</span> <span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;_&quot;</span><span class="p">)</span> \
                <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;&#39;&quot;</span> <span class="ow">and</span> <span class="n">v</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;&#39;&quot;</span><span class="p">)</span> \
                <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;&quot;&#39;</span> <span class="ow">and</span> <span class="n">v</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;&quot;&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="s2">&quot;&#39;&quot;</span> <span class="ow">in</span> <span class="n">v</span><span class="p">:</span>
                <span class="n">q</span> <span class="o">=</span> <span class="s1">&#39;&quot;&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">q</span> <span class="o">=</span> <span class="s2">&quot;&#39;&quot;</span>
            <span class="n">v</span> <span class="o">=</span> <span class="n">q</span> <span class="o">+</span> <span class="n">v</span> <span class="o">+</span> <span class="n">q</span>
        <span class="k">return</span> <span class="n">v</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_process_string</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">string</span><span class="p">):</span>
        <span class="c1"># remove comments</span>
        <span class="n">string</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;(\s|^)#.*$&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">string</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span><span class="p">)</span>
        <span class="c1"># remove empty lines</span>
        <span class="n">string</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;^\s*\n&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">string</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span><span class="p">)</span>
        <span class="c1"># remove non_ascii</span>
        <span class="n">string</span> <span class="o">=</span> <span class="n">remove_non_ascii</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
        <span class="c1"># since line breaks in .cif files are mostly meaningless,</span>
        <span class="c1"># break up into a stream of tokens to parse, rejoining multiline</span>
        <span class="c1"># strings (between semicolons)</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">deque</span><span class="p">()</span>
        <span class="n">multiline</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">ml</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># this regex splits on spaces, except when in quotes.</span>
        <span class="c1"># starting quotes must not be preceded by non-whitespace</span>
        <span class="c1"># (these get eaten by the first expression)</span>
        <span class="c1"># ending quotes must not be followed by non-whitespace</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;&#39;&#39;([^&#39;&quot;\s][\S]*)|&#39;(.*?)&#39;(?!\S)|&quot;(.*?)&quot;(?!\S)&#39;&#39;&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">string</span><span class="o">.</span><span class="n">splitlines</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">multiline</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">):</span>
                    <span class="n">multiline</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="n">q</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ml</span><span class="p">)))</span>
                    <span class="n">ml</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">ml</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
                    <span class="k">continue</span>
            <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">):</span>
                <span class="n">multiline</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">ml</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">p</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
                    <span class="c1"># s is tuple. location of the data in the tuple</span>
                    <span class="c1"># depends on whether it was quoted in the input</span>
                    <span class="n">q</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">q</span>

<div class="viewcode-block" id="CifBlock.from_string"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifBlock.from_string">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_string</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">string</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reads CifBlock from string.</span>

<span class="sd">        :param string: String representation.</span>
<span class="sd">        :return: CifBlock</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">q</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_process_string</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
        <span class="n">header</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">popleft</span><span class="p">()[</span><span class="mi">0</span><span class="p">][</span><span class="mi">5</span><span class="p">:]</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="n">loops</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">while</span> <span class="n">q</span><span class="p">:</span>
            <span class="n">s</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>
            <span class="c1"># cif keys aren&#39;t in quotes, so show up in s[0]</span>
            <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;_eof&quot;</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">data</span><span class="p">[</span><span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">popleft</span><span class="p">())</span>
                <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                    <span class="n">data</span><span class="p">[</span><span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
            <span class="k">elif</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;loop_&quot;</span><span class="p">):</span>
                <span class="n">columns</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">while</span> <span class="n">q</span><span class="p">:</span>
                    <span class="n">s</span> <span class="o">=</span> <span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;loop_&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">):</span>
                        <span class="k">break</span>
                    <span class="n">columns</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">popleft</span><span class="p">()))</span>
                    <span class="n">data</span><span class="p">[</span><span class="n">columns</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">while</span> <span class="n">q</span><span class="p">:</span>
                    <span class="n">s</span> <span class="o">=</span> <span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;loop_&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">):</span>
                        <span class="k">break</span>
                    <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">popleft</span><span class="p">()))</span>
                <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">items</span><span class="p">)</span> <span class="o">//</span> <span class="nb">len</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span>
                <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">items</span><span class="p">)</span> <span class="o">%</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span>
                <span class="n">loops</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">columns</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span> <span class="n">items</span><span class="p">):</span>
                    <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
            <span class="k">elif</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span><span class="p">:</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Possible issue in cif file&quot;</span>
                              <span class="s2">&quot; at line: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()))</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">loops</span><span class="p">,</span> <span class="n">header</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="CifFile"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifFile">[docs]</a><span class="k">class</span> <span class="nc">CifFile</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Reads and parses CifBlocks from a .cif file or string</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">orig_string</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            data (OrderedDict): Of CifBlock objects.å</span>
<span class="sd">            orig_string (str): The original cif string.</span>
<span class="sd">            comment (str): Comment string.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">orig_string</span> <span class="o">=</span> <span class="n">orig_string</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">comment</span> <span class="o">=</span> <span class="n">comment</span> <span class="ow">or</span> <span class="s2">&quot;# generated using pymatgen&quot;</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">comment</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>

<div class="viewcode-block" id="CifFile.from_string"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifFile.from_string">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_string</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">string</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reads CifFile from a string.</span>

<span class="sd">        :param string: String representation.</span>
<span class="sd">        :return: CifFile</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;^\s*data_&quot;</span><span class="p">,</span> <span class="s2">&quot;x</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">string</span><span class="p">,</span>
                          <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)[</span><span class="mi">1</span><span class="p">:]:</span>

            <span class="c1"># Skip over Cif block that contains powder diffraction data.</span>
            <span class="c1"># Some elements in this block were missing from CIF files in</span>
            <span class="c1"># Springer materials/Pauling file DBs.</span>
            <span class="c1"># This block anyway does not contain any structure information, and</span>
            <span class="c1"># CifParser was also not parsing it.</span>
            <span class="k">if</span> <span class="s1">&#39;powder_pattern&#39;</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\n&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]:</span>
                <span class="k">continue</span>
            <span class="n">c</span> <span class="o">=</span> <span class="n">CifBlock</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="s2">&quot;data_&quot;</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span>
            <span class="n">d</span><span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">header</span><span class="p">]</span> <span class="o">=</span> <span class="n">c</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">string</span><span class="p">)</span></div>

<div class="viewcode-block" id="CifFile.from_file"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifFile.from_file">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_file</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reads CifFile from a filename.</span>

<span class="sd">        :param filename: Filename</span>
<span class="sd">        :return: CifFile</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">zopen</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">filename</span><span class="p">),</span> <span class="s2">&quot;rt&quot;</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s2">&quot;replace&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></div></div>


<div class="viewcode-block" id="CifParser"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser">[docs]</a><span class="k">class</span> <span class="nc">CifParser</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parses a CIF file. Attempts to fix CIFs that are out-of-spec, but will</span>
<span class="sd">    issue warnings if corrections applied. These are also stored in the</span>
<span class="sd">    CifParser&#39;s errors attribute.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">occupancy_tolerance</span><span class="o">=</span><span class="mf">1.</span><span class="p">,</span> <span class="n">site_tolerance</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            filename (str): CIF filename, bzipped or gzipped CIF files are fine too.</span>
<span class="sd">            occupancy_tolerance (float): If total occupancy of a site is between 1</span>
<span class="sd">                and occupancy_tolerance, the occupancies will be scaled down to 1.</span>
<span class="sd">            site_tolerance (float): This tolerance is used to determine if two</span>
<span class="sd">                sites are sitting in the same position, in which case they will be</span>
<span class="sd">                combined to a single disordered site. Defaults to 1e-4.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_occupancy_tolerance</span> <span class="o">=</span> <span class="n">occupancy_tolerance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_site_tolerance</span> <span class="o">=</span> <span class="n">site_tolerance</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">Path</span><span class="p">)):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span> <span class="o">=</span> <span class="n">CifFile</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span> <span class="o">=</span> <span class="n">CifFile</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">filename</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>

        <span class="c1"># store if CIF contains features from non-core CIF dictionaries</span>
        <span class="c1"># e.g. magCIF</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">def</span> <span class="nf">is_magcif</span><span class="p">():</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Checks to see if file appears to be a magCIF file (heuristic).</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="c1"># Doesn&#39;t seem to be a canonical way to test if file is magCIF or</span>
            <span class="c1"># not, so instead check for magnetic symmetry datanames</span>
            <span class="n">prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;_space_group_magn&#39;</span><span class="p">,</span> <span class="s1">&#39;_atom_site_moment&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;_space_group_symop_magn&#39;</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="k">for</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="n">prefixes</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="n">k</span><span class="p">:</span>
                            <span class="k">return</span> <span class="kc">True</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s1">&#39;magcif&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">is_magcif</span><span class="p">()</span>

        <span class="k">def</span> <span class="nf">is_magcif_incommensurate</span><span class="p">():</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Checks to see if file contains an incommensurate magnetic</span>
<span class="sd">            structure (heuristic).</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="c1"># Doesn&#39;t seem to be a canonical way to test if magCIF file</span>
            <span class="c1"># describes incommensurate strucure or not, so instead check</span>
            <span class="c1"># for common datanames</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif&quot;</span><span class="p">]:</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="n">prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;_cell_modulation_dimension&#39;</span><span class="p">,</span> <span class="s1">&#39;_cell_wave_vector&#39;</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="k">for</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="n">prefixes</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="n">k</span><span class="p">:</span>
                            <span class="k">return</span> <span class="kc">True</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s1">&#39;magcif_incommensurate&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">is_magcif_incommensurate</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="c1"># pass individual CifBlocks to _sanitize_data</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sanitize_data</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>

<div class="viewcode-block" id="CifParser.from_string"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.from_string">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">from_string</span><span class="p">(</span><span class="n">cif_string</span><span class="p">,</span> <span class="n">occupancy_tolerance</span><span class="o">=</span><span class="mf">1.</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates a CifParser from a string.</span>

<span class="sd">        Args:</span>
<span class="sd">            cif_string (str): String representation of a CIF.</span>
<span class="sd">            occupancy_tolerance (float): If total occupancy of a site is</span>
<span class="sd">                between 1 and occupancy_tolerance, the occupancies will be</span>
<span class="sd">                scaled down to 1.</span>

<span class="sd">        Returns:</span>
<span class="sd">            CifParser</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">stream</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="n">cif_string</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">CifParser</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">occupancy_tolerance</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_sanitize_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Some CIF files do not conform to spec. This function corrects</span>
<span class="sd">        known issues, particular in regards to Springer materials/</span>
<span class="sd">        Pauling files.</span>

<span class="sd">        This function is here so that CifParser can assume its</span>
<span class="sd">        input conforms to spec, simplifying its implementation.</span>
<span class="sd">        :param data: CifBlock</span>
<span class="sd">        :return: data CifBlock</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This part of the code deals with handling formats of data as found in</span>
<span class="sd">        CIF files extracted from the Springer Materials/Pauling File</span>
<span class="sd">        databases, and that are different from standard ICSD formats.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># check for implicit hydrogens, warn if any present</span>
        <span class="k">if</span> <span class="s2">&quot;_atom_site_attached_hydrogens&quot;</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">attached_hydrogens</span> <span class="o">=</span> <span class="p">[</span><span class="n">str2float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;_atom_site_attached_hydrogens&#39;</span><span class="p">]</span>
                                  <span class="k">if</span> <span class="n">str2float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">attached_hydrogens</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Structure has implicit hydrogens defined, &quot;</span>
                                     <span class="s2">&quot;parsed structure unlikely to be suitable for use &quot;</span>
                                     <span class="s2">&quot;in calculations unless hydrogens added.&quot;</span><span class="p">)</span>

        <span class="c1"># Check to see if &quot;_atom_site_type_symbol&quot; exists, as some test CIFs do</span>
        <span class="c1"># not contain this key.</span>
        <span class="k">if</span> <span class="s2">&quot;_atom_site_type_symbol&quot;</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>

            <span class="c1"># Keep a track of which data row needs to be removed.</span>
            <span class="c1"># Example of a row: Nb,Zr &#39;0.8Nb + 0.2Zr&#39; .2a .m-3m 0 0 0 1 14</span>
            <span class="c1"># &#39;rhombic dodecahedron, Nb&lt;sub&gt;14&lt;/sub&gt;&#39;</span>
            <span class="c1"># Without this code, the above row in a structure would be parsed</span>
            <span class="c1"># as an ordered site with only Nb (since</span>
            <span class="c1"># CifParser would try to parse the first two characters of the</span>
            <span class="c1"># label &quot;Nb,Zr&quot;) and occupancy=1.</span>
            <span class="c1"># However, this site is meant to be a disordered site with 0.8 of</span>
            <span class="c1"># Nb and 0.2 of Zr.</span>
            <span class="n">idxs_to_remove</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="n">new_atom_site_label</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">new_atom_site_type_symbol</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">new_atom_site_occupancy</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">new_fract_x</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">new_fract_y</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">new_fract_z</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">el_row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">]):</span>

                <span class="c1"># CIF files from the Springer Materials/Pauling File have</span>
                <span class="c1"># switched the label and symbol. Thus, in the</span>
                <span class="c1"># above shown example row, &#39;0.8Nb + 0.2Zr&#39; is the symbol.</span>
                <span class="c1"># Below, we split the strings on &#39; + &#39; to</span>
                <span class="c1"># check if the length (or number of elements) in the label and</span>
                <span class="c1"># symbol are equal.</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">][</span><span class="n">idx</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; + &#39;</span><span class="p">))</span> <span class="o">&gt;</span> \
                        <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">][</span><span class="n">idx</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; + &#39;</span><span class="p">)):</span>

                    <span class="c1"># Dictionary to hold extracted elements and occupancies</span>
                    <span class="n">els_occu</span> <span class="o">=</span> <span class="p">{}</span>

                    <span class="c1"># parse symbol to get element names and occupancy and store</span>
                    <span class="c1"># in &quot;els_occu&quot;</span>
                    <span class="n">symbol_str</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">][</span><span class="n">idx</span><span class="p">]</span>
                    <span class="n">symbol_str_lst</span> <span class="o">=</span> <span class="n">symbol_str</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; + &#39;</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">elocc_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">symbol_str_lst</span><span class="p">)):</span>
                        <span class="c1"># Remove any bracketed items in the string</span>
                        <span class="n">symbol_str_lst</span><span class="p">[</span><span class="n">elocc_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span>
                            <span class="sa">r</span><span class="s1">&#39;\([0-9]*\)&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                            <span class="n">symbol_str_lst</span><span class="p">[</span><span class="n">elocc_idx</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>

                        <span class="c1"># Extract element name and its occupancy from the</span>
                        <span class="c1"># string, and store it as a</span>
                        <span class="c1"># key-value pair in &quot;els_occ&quot;.</span>
                        <span class="n">els_occu</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;\D+&#39;</span><span class="p">,</span> <span class="n">symbol_str_lst</span><span class="p">[</span>
                            <span class="n">elocc_idx</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">())[</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;&lt;sup&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)]</span> <span class="o">=</span> \
                            <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;0&#39;</span> <span class="o">+</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;\.?\d+&#39;</span><span class="p">,</span> <span class="n">symbol_str_lst</span><span class="p">[</span>
                                <span class="n">elocc_idx</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">())[</span><span class="mi">1</span><span class="p">])</span>

                    <span class="n">x</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_x&quot;</span><span class="p">][</span><span class="n">idx</span><span class="p">])</span>
                    <span class="n">y</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_y&quot;</span><span class="p">][</span><span class="n">idx</span><span class="p">])</span>
                    <span class="n">z</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_z&quot;</span><span class="p">][</span><span class="n">idx</span><span class="p">])</span>

                    <span class="k">for</span> <span class="n">et</span><span class="p">,</span> <span class="n">occu</span> <span class="ow">in</span> <span class="n">els_occu</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                        <span class="c1"># new atom site labels have &#39;fix&#39; appended</span>
                        <span class="n">new_atom_site_label</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="n">et</span> <span class="o">+</span> <span class="s1">&#39;_fix&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">new_atom_site_label</span><span class="p">)))</span>
                        <span class="n">new_atom_site_type_symbol</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">et</span><span class="p">)</span>
                        <span class="n">new_atom_site_occupancy</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">occu</span><span class="p">))</span>
                        <span class="n">new_fract_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
                        <span class="n">new_fract_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
                        <span class="n">new_fract_z</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>

                    <span class="n">idxs_to_remove</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>

            <span class="c1"># Remove the original row by iterating over all keys in the CIF</span>
            <span class="c1"># data looking for lists, which indicates</span>
            <span class="c1"># multiple data items, one for each row, and remove items from the</span>
            <span class="c1"># list that corresponds to the removed row,</span>
            <span class="c1"># so that it&#39;s not processed by the rest of this function (which</span>
            <span class="c1"># would result in an error).</span>
            <span class="k">for</span> <span class="n">original_key</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">original_key</span><span class="p">],</span> <span class="nb">list</span><span class="p">):</span>
                    <span class="k">for</span> <span class="nb">id</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">idxs_to_remove</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
                        <span class="k">del</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">original_key</span><span class="p">][</span><span class="nb">id</span><span class="p">]</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">idxs_to_remove</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Pauling file corrections applied.&quot;</span><span class="p">)</span>

                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">new_atom_site_label</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">new_atom_site_type_symbol</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_occupancy&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">new_atom_site_occupancy</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_x&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">new_fract_x</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_y&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">new_fract_y</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_z&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">new_fract_z</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This fixes inconsistencies in naming of several magCIF tags</span>
<span class="sd">        as a result of magCIF being in widespread use prior to</span>
<span class="sd">        specification being finalized (on advice of Branton Campbell).</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif&quot;</span><span class="p">]:</span>

            <span class="c1"># CIF-1 style has all underscores, interim standard</span>
            <span class="c1"># had period before magn instead of before the final</span>
            <span class="c1"># component (e.g. xyz)</span>
            <span class="c1"># we want to standardize on a specific key, to simplify</span>
            <span class="c1"># parsing code</span>
            <span class="n">correct_keys</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;_space_group_symop_magn_operation.xyz&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_space_group_symop_magn_centering.xyz&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_space_group_magn.name_BNS&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_space_group_magn.number_BNS&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_atom_site_moment_crystalaxis_x&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_atom_site_moment_crystalaxis_y&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_atom_site_moment_crystalaxis_z&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;_atom_site_moment_label&quot;</span><span class="p">]</span>

            <span class="c1"># cannot mutate OrderedDict during enumeration,</span>
            <span class="c1"># so store changes we want to make</span>
            <span class="n">changes_to_make</span> <span class="o">=</span> <span class="p">{}</span>

            <span class="k">for</span> <span class="n">original_key</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">correct_key</span> <span class="ow">in</span> <span class="n">correct_keys</span><span class="p">:</span>
                    <span class="c1"># convert to all underscore</span>
                    <span class="n">trial_key</span> <span class="o">=</span> <span class="s2">&quot;_&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">correct_key</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">))</span>
                    <span class="n">test_key</span> <span class="o">=</span> <span class="s2">&quot;_&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">original_key</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">trial_key</span> <span class="o">==</span> <span class="n">test_key</span><span class="p">:</span>
                        <span class="n">changes_to_make</span><span class="p">[</span><span class="n">correct_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">original_key</span>

            <span class="c1"># make changes</span>
            <span class="k">for</span> <span class="n">correct_key</span><span class="p">,</span> <span class="n">original_key</span> <span class="ow">in</span> <span class="n">changes_to_make</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">correct_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">original_key</span><span class="p">]</span>

            <span class="c1"># renamed_keys maps interim_keys to final_keys</span>
            <span class="n">renamed_keys</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s2">&quot;_magnetic_space_group.transform_to_standard_Pp_abc&quot;</span><span class="p">:</span>
                    <span class="s2">&quot;_space_group_magn.transform_BNS_Pp_abc&quot;</span><span class="p">}</span>
            <span class="n">changes_to_make</span> <span class="o">=</span> <span class="p">{}</span>

            <span class="k">for</span> <span class="n">interim_key</span><span class="p">,</span> <span class="n">final_key</span> <span class="ow">in</span> <span class="n">renamed_keys</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">interim_key</span><span class="p">):</span>
                    <span class="n">changes_to_make</span><span class="p">[</span><span class="n">final_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">interim_key</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">changes_to_make</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Keys changed to match new magCIF specification.&quot;</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">final_key</span><span class="p">,</span> <span class="n">interim_key</span> <span class="ow">in</span> <span class="n">changes_to_make</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">final_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">interim_key</span><span class="p">]</span>

        <span class="c1"># check for finite precision frac co-ordinates (e.g. 0.6667 instead of 0.6666666...7)</span>
        <span class="c1"># this can sometimes cause serious issues when applying symmetry operations</span>
        <span class="n">important_fracs</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">,</span> <span class="mi">2</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="n">fracs_to_change</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">label</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;_atom_site_fract_x&#39;</span><span class="p">,</span> <span class="s1">&#39;_atom_site_fract_y&#39;</span><span class="p">,</span> <span class="s1">&#39;_atom_site_fract_z&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">label</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">frac</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">label</span><span class="p">]):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">frac</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">frac</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                        <span class="c1"># co-ordinate might not be defined e.g. &#39;?&#39;</span>
                        <span class="k">continue</span>
                    <span class="k">for</span> <span class="n">comparison_frac</span> <span class="ow">in</span> <span class="n">important_fracs</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">frac</span> <span class="o">/</span> <span class="n">comparison_frac</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-4</span><span class="p">:</span>
                            <span class="n">fracs_to_change</span><span class="p">[(</span><span class="n">label</span><span class="p">,</span> <span class="n">idx</span><span class="p">)]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">comparison_frac</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">fracs_to_change</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Some fractional co-ordinates rounded to ideal values to &quot;</span>
                                 <span class="s2">&quot;avoid issues with finite precision.&quot;</span><span class="p">)</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">idx</span><span class="p">),</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">fracs_to_change</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">label</span><span class="p">][</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>

        <span class="k">return</span> <span class="n">data</span>

    <span class="k">def</span> <span class="nf">_unique_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">coords_in</span><span class="p">,</span> <span class="n">magmoms_in</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate unique coordinates using coord and symmetry positions</span>
<span class="sd">        and also their corresponding magnetic moments, if supplied.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">coords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">magmoms_in</span><span class="p">:</span>
            <span class="n">magmoms</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">magmoms_in</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords_in</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span>
            <span class="k">for</span> <span class="n">tmp_coord</span><span class="p">,</span> <span class="n">tmp_magmom</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">coords_in</span><span class="p">,</span> <span class="n">magmoms_in</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">symmetry_operations</span><span class="p">:</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">op</span><span class="o">.</span><span class="n">operate</span><span class="p">(</span><span class="n">tmp_coord</span><span class="p">)</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">i</span> <span class="o">-</span> <span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">coord</span><span class="p">])</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="n">MagSymmOp</span><span class="p">):</span>
                        <span class="c1"># Up to this point, magmoms have been defined relative</span>
                        <span class="c1"># to crystal axis. Now convert to Cartesian and into</span>
                        <span class="c1"># a Magmom object.</span>
                        <span class="n">magmom</span> <span class="o">=</span> <span class="n">Magmom</span><span class="o">.</span><span class="n">from_moment_relative_to_crystal_axes</span><span class="p">(</span>
                            <span class="n">op</span><span class="o">.</span><span class="n">operate_magmom</span><span class="p">(</span><span class="n">tmp_magmom</span><span class="p">),</span>
                            <span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">magmom</span> <span class="o">=</span> <span class="n">Magmom</span><span class="p">(</span><span class="n">tmp_magmom</span><span class="p">)</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">in_coord_list_pbc</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">coord</span><span class="p">,</span>
                                             <span class="n">atol</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_site_tolerance</span><span class="p">):</span>
                        <span class="n">coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>
                        <span class="n">magmoms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">magmom</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">coords</span><span class="p">,</span> <span class="n">magmoms</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">tmp_coord</span> <span class="ow">in</span> <span class="n">coords_in</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">symmetry_operations</span><span class="p">:</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">op</span><span class="o">.</span><span class="n">operate</span><span class="p">(</span><span class="n">tmp_coord</span><span class="p">)</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">i</span> <span class="o">-</span> <span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">coord</span><span class="p">])</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">in_coord_list_pbc</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">coord</span><span class="p">,</span>
                                             <span class="n">atol</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_site_tolerance</span><span class="p">):</span>
                        <span class="n">coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">coords</span><span class="p">,</span> <span class="p">[</span><span class="n">Magmom</span><span class="p">(</span><span class="mi">0</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span>  <span class="c1"># return dummy magmoms</span>

<div class="viewcode-block" id="CifParser.get_lattice"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.get_lattice">[docs]</a>    <span class="k">def</span> <span class="nf">get_lattice</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">length_strings</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="p">),</span>
                    <span class="n">angle_strings</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="s2">&quot;beta&quot;</span><span class="p">,</span> <span class="s2">&quot;gamma&quot;</span><span class="p">),</span>
                    <span class="n">lattice_type</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate the lattice from the provided lattice parameters. In</span>
<span class="sd">        the absence of all six lattice parameters, the crystal system</span>
<span class="sd">        and necessary parameters are parsed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>

            <span class="n">lengths</span> <span class="o">=</span> <span class="p">[</span><span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_cell_length_&quot;</span> <span class="o">+</span> <span class="n">i</span><span class="p">])</span>
                       <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">length_strings</span><span class="p">]</span>
            <span class="n">angles</span> <span class="o">=</span> <span class="p">[</span><span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_cell_angle_&quot;</span> <span class="o">+</span> <span class="n">i</span><span class="p">])</span>
                      <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">angle_strings</span><span class="p">]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">lattice_type</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_parameters</span><span class="p">(</span><span class="o">*</span><span class="n">lengths</span><span class="p">,</span> <span class="o">*</span><span class="n">angles</span><span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">Lattice</span><span class="p">,</span> <span class="n">lattice_type</span><span class="p">)(</span><span class="o">*</span><span class="p">(</span><span class="n">lengths</span> <span class="o">+</span> <span class="n">angles</span><span class="p">))</span>

        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="c1"># Missing Key search for cell setting</span>
            <span class="k">for</span> <span class="n">lattice_lable</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;_symmetry_cell_setting&quot;</span><span class="p">,</span>
                                  <span class="s2">&quot;_space_group_crystal_system&quot;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">lattice_lable</span><span class="p">):</span>
                    <span class="n">lattice_type</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">lattice_lable</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                    <span class="k">try</span><span class="p">:</span>

                        <span class="n">required_args</span> <span class="o">=</span> <span class="n">getargspec</span><span class="p">(</span>
                            <span class="nb">getattr</span><span class="p">(</span><span class="n">Lattice</span><span class="p">,</span> <span class="n">lattice_type</span><span class="p">))</span><span class="o">.</span><span class="n">args</span>

                        <span class="n">lengths</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">length_strings</span>
                                   <span class="k">if</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">required_args</span><span class="p">)</span>
                        <span class="n">angles</span> <span class="o">=</span> <span class="p">(</span><span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">angle_strings</span>
                                  <span class="k">if</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">required_args</span><span class="p">)</span>
                        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_lattice</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">lengths</span><span class="p">,</span> <span class="n">angles</span><span class="p">,</span>
                                                <span class="n">lattice_type</span><span class="o">=</span><span class="n">lattice_type</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">AttributeError</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
                        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">exc</span><span class="p">)</span>

                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="CifParser.get_symops"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.get_symops">[docs]</a>    <span class="k">def</span> <span class="nf">get_symops</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        In order to generate symmetry equivalent positions, the symmetry</span>
<span class="sd">        operations are parsed. If the symops are not present, the space</span>
<span class="sd">        group symbol is parsed, and symops are generated.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">symops</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">symmetry_label</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;_symmetry_equiv_pos_as_xyz&quot;</span><span class="p">,</span>
                               <span class="s2">&quot;_symmetry_equiv_pos_as_xyz_&quot;</span><span class="p">,</span>
                               <span class="s2">&quot;_space_group_symop_operation_xyz&quot;</span><span class="p">,</span>
                               <span class="s2">&quot;_space_group_symop_operation_xyz_&quot;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symmetry_label</span><span class="p">):</span>
                <span class="n">xyz</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symmetry_label</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xyz</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;A 1-line symmetry op P1 CIF is detected!&quot;</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                    <span class="n">xyz</span> <span class="o">=</span> <span class="p">[</span><span class="n">xyz</span><span class="p">]</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">symops</span> <span class="o">=</span> <span class="p">[</span><span class="n">SymmOp</span><span class="o">.</span><span class="n">from_xyz_string</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                              <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">xyz</span><span class="p">]</span>
                    <span class="k">break</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">continue</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">symops</span><span class="p">:</span>
            <span class="c1"># Try to parse symbol</span>
            <span class="k">for</span> <span class="n">symmetry_label</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;_symmetry_space_group_name_H-M&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_H_M&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_H-M_&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_H_M_&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_space_group_name_Hall&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_space_group_name_Hall_&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_space_group_name_H-M_alt&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_space_group_name_H-M_alt_&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_hall&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_hall_&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_h-m&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_space_group_name_h-m_&quot;</span><span class="p">]:</span>
                <span class="n">sg</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symmetry_label</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">sg</span><span class="p">:</span>
                    <span class="n">sg</span> <span class="o">=</span> <span class="n">sub_spgrp</span><span class="p">(</span><span class="n">sg</span><span class="p">)</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">spg</span> <span class="o">=</span> <span class="n">space_groups</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sg</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">spg</span><span class="p">:</span>
                            <span class="n">symops</span> <span class="o">=</span> <span class="n">SpaceGroup</span><span class="p">(</span><span class="n">spg</span><span class="p">)</span><span class="o">.</span><span class="n">symmetry_ops</span>
                            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;No _symmetry_equiv_pos_as_xyz type key found. &quot;</span> \
                                  <span class="s2">&quot;Spacegroup from </span><span class="si">%s</span><span class="s2"> used.&quot;</span> <span class="o">%</span> <span class="n">symmetry_label</span>
                            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                            <span class="k">break</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="c1"># Ignore any errors</span>
                        <span class="k">pass</span>

                    <span class="k">try</span><span class="p">:</span>
                        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">_get_cod_data</span><span class="p">():</span>
                            <span class="k">if</span> <span class="n">sg</span> <span class="o">==</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\s+&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span>
                                            <span class="n">d</span><span class="p">[</span><span class="s2">&quot;hermann_mauguin&quot;</span><span class="p">]):</span>
                                <span class="n">xyz</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;symops&quot;</span><span class="p">]</span>
                                <span class="n">symops</span> <span class="o">=</span> <span class="p">[</span><span class="n">SymmOp</span><span class="o">.</span><span class="n">from_xyz_string</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                                          <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">xyz</span><span class="p">]</span>
                                <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;No _symmetry_equiv_pos_as_xyz type key found. &quot;</span> \
                                      <span class="s2">&quot;Spacegroup from </span><span class="si">%s</span><span class="s2"> used.&quot;</span> <span class="o">%</span> <span class="n">symmetry_label</span>
                                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                                <span class="k">break</span>
                    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                        <span class="k">continue</span>

                    <span class="k">if</span> <span class="n">symops</span><span class="p">:</span>
                        <span class="k">break</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">symops</span><span class="p">:</span>
            <span class="c1"># Try to parse International number</span>
            <span class="k">for</span> <span class="n">symmetry_label</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;_space_group_IT_number&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_space_group_IT_number_&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_Int_Tables_number&quot;</span><span class="p">,</span>
                                   <span class="s2">&quot;_symmetry_Int_Tables_number_&quot;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symmetry_label</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">i</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symmetry_label</span><span class="p">)))</span>
                        <span class="n">symops</span> <span class="o">=</span> <span class="n">SpaceGroup</span><span class="o">.</span><span class="n">from_int_number</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">symmetry_ops</span>
                        <span class="k">break</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="k">continue</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">symops</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;No _symmetry_equiv_pos_as_xyz type key found. &quot;</span> \
                  <span class="s2">&quot;Defaulting to P1.&quot;</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="n">symops</span> <span class="o">=</span> <span class="p">[</span><span class="n">SymmOp</span><span class="o">.</span><span class="n">from_xyz_string</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;z&#39;</span><span class="p">]]</span>

        <span class="k">return</span> <span class="n">symops</span></div>

<div class="viewcode-block" id="CifParser.get_magsymops"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.get_magsymops">[docs]</a>    <span class="k">def</span> <span class="nf">get_magsymops</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Equivalent to get_symops except for magnetic symmetry groups.</span>
<span class="sd">        Separate function since additional operation for time reversal symmetry</span>
<span class="sd">        (which changes magnetic moments on sites) needs to be returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">magsymmops</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># check to see if magCIF file explicitly contains magnetic symmetry operations</span>
        <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_symop_magn_operation.xyz&quot;</span><span class="p">):</span>

            <span class="n">xyzt</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_symop_magn_operation.xyz&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xyzt</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="n">xyzt</span> <span class="o">=</span> <span class="p">[</span><span class="n">xyzt</span><span class="p">]</span>
            <span class="n">magsymmops</span> <span class="o">=</span> <span class="p">[</span><span class="n">MagSymmOp</span><span class="o">.</span><span class="n">from_xyzt_string</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">xyzt</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_symop_magn_centering.xyz&quot;</span><span class="p">):</span>

                <span class="n">xyzt</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_symop_magn_centering.xyz&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xyzt</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="n">xyzt</span> <span class="o">=</span> <span class="p">[</span><span class="n">xyzt</span><span class="p">]</span>
                <span class="n">centering_symops</span> <span class="o">=</span> <span class="p">[</span><span class="n">MagSymmOp</span><span class="o">.</span><span class="n">from_xyzt_string</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">xyzt</span><span class="p">]</span>

                <span class="n">all_ops</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="n">magsymmops</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">centering_op</span> <span class="ow">in</span> <span class="n">centering_symops</span><span class="p">:</span>
                        <span class="n">new_translation</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span>
                                           <span class="ow">in</span>
                                           <span class="n">op</span><span class="o">.</span><span class="n">translation_vector</span> <span class="o">+</span> <span class="n">centering_op</span><span class="o">.</span><span class="n">translation_vector</span><span class="p">]</span>
                        <span class="n">new_time_reversal</span> <span class="o">=</span> <span class="n">op</span><span class="o">.</span><span class="n">time_reversal</span> <span class="o">*</span> <span class="n">centering_op</span><span class="o">.</span><span class="n">time_reversal</span>
                        <span class="n">all_ops</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="n">MagSymmOp</span><span class="o">.</span><span class="n">from_rotation_and_translation_and_time_reversal</span><span class="p">(</span>
                                <span class="n">rotation_matrix</span><span class="o">=</span><span class="n">op</span><span class="o">.</span><span class="n">rotation_matrix</span><span class="p">,</span>
                                <span class="n">translation_vec</span><span class="o">=</span><span class="n">new_translation</span><span class="p">,</span>
                                <span class="n">time_reversal</span><span class="o">=</span><span class="n">new_time_reversal</span><span class="p">))</span>
                <span class="n">magsymmops</span> <span class="o">=</span> <span class="n">all_ops</span>

        <span class="c1"># else check to see if it specifies a magnetic space group</span>
        <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.name_BNS&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
                <span class="s2">&quot;_space_group_magn.number_BNS&quot;</span><span class="p">):</span>

            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.name_BNS&quot;</span><span class="p">):</span>
                <span class="c1"># get BNS label for MagneticSpaceGroup()</span>
                <span class="nb">id</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.name_BNS&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># get BNS number for MagneticSpaceGroup()</span>
                <span class="c1"># by converting string to list of ints</span>
                <span class="nb">id</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="p">(</span>
                    <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.number_BNS&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">))))</span>

            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.transform_BNS_Pp_abc&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
                        <span class="s2">&quot;_space_group_magn.transform_BNS_Pp_abc&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="s2">&quot;a,b,c;0,0,0&quot;</span><span class="p">:</span>

                    <span class="n">jf</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.transform_BNS_Pp_abc&quot;</span><span class="p">)</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="n">MagneticSpaceGroup</span><span class="p">(</span><span class="nb">id</span><span class="p">,</span> <span class="n">jf</span><span class="p">)</span>

            <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;_space_group_magn.transform_BNS_Pp&quot;</span><span class="p">):</span>
                <span class="k">return</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                    <span class="s2">&quot;Incomplete specification to implement.&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="n">MagneticSpaceGroup</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>

            <span class="n">magsymmops</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">symmetry_ops</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">magsymmops</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;No magnetic symmetry detected, using primitive symmetry.&quot;</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="n">magsymmops</span> <span class="o">=</span> <span class="p">[</span><span class="n">MagSymmOp</span><span class="o">.</span><span class="n">from_xyzt_string</span><span class="p">(</span><span class="s2">&quot;x, y, z, 1&quot;</span><span class="p">)]</span>

        <span class="k">return</span> <span class="n">magsymmops</span></div>

<div class="viewcode-block" id="CifParser.parse_oxi_states"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.parse_oxi_states">[docs]</a>    <span class="k">def</span> <span class="nf">parse_oxi_states</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parse oxidation states from data dictionary</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">oxi_states</span> <span class="o">=</span> <span class="p">{</span>
                <span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_type_symbol&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]:</span>
                    <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_type_oxidation_number&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_type_symbol&quot;</span><span class="p">]))}</span>
            <span class="c1"># attempt to strip oxidation state from _atom_type_symbol</span>
            <span class="c1"># in case the label does not contain an oxidation state</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_type_symbol&quot;</span><span class="p">]):</span>
                <span class="n">oxi_states</span><span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\d?[\+,\-]?$&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">symbol</span><span class="p">)]</span> <span class="o">=</span> \
                    <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_type_oxidation_number&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>

        <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
            <span class="n">oxi_states</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">oxi_states</span></div>

<div class="viewcode-block" id="CifParser.parse_magmoms"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.parse_magmoms">[docs]</a>    <span class="k">def</span> <span class="nf">parse_magmoms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parse atomic magnetic moments from data dictionary</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">lattice</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span>
                <span class="s1">&#39;Magmoms given in terms of crystal axes in magCIF spec.&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">magmoms</span> <span class="o">=</span> <span class="p">{</span>
                <span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_moment_label&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]:</span>
                    <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span>
                        <span class="p">[</span><span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_moment_crystalaxis_x&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]),</span>
                         <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_moment_crystalaxis_y&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]),</span>
                         <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_moment_crystalaxis_z&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])]</span>
                    <span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_moment_label&quot;</span><span class="p">]))</span>
            <span class="p">}</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">magmoms</span></div>

    <span class="k">def</span> <span class="nf">_parse_symbol</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sym</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parse a string with a symbol to extract a string representing an element.</span>

<span class="sd">        Args:</span>
<span class="sd">            sym (str): A symbol to be parsed.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A string with the parsed symbol. None if no parsing was possible.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Common representations for elements/water in cif files</span>
        <span class="c1"># TODO: fix inconsistent handling of water</span>
        <span class="n">special</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;Hw&quot;</span><span class="p">:</span> <span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;Ow&quot;</span><span class="p">:</span> <span class="s2">&quot;O&quot;</span><span class="p">,</span> <span class="s2">&quot;Wat&quot;</span><span class="p">:</span> <span class="s2">&quot;O&quot;</span><span class="p">,</span>
                   <span class="s2">&quot;wat&quot;</span><span class="p">:</span> <span class="s2">&quot;O&quot;</span><span class="p">,</span> <span class="s2">&quot;OH&quot;</span><span class="p">:</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;OH2&quot;</span><span class="p">:</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;NO3&quot;</span><span class="p">:</span> <span class="s2">&quot;N&quot;</span><span class="p">}</span>

        <span class="n">parsed_sym</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="c1"># try with special symbols, otherwise check the first two letters,</span>
        <span class="c1"># then the first letter alone. If everything fails try extracting the</span>
        <span class="c1"># first letters.</span>
        <span class="n">m_sp</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;|&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">special</span><span class="o">.</span><span class="n">keys</span><span class="p">()),</span> <span class="n">sym</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">m_sp</span><span class="p">:</span>
            <span class="n">parsed_sym</span> <span class="o">=</span> <span class="n">special</span><span class="p">[</span><span class="n">m_sp</span><span class="o">.</span><span class="n">group</span><span class="p">()]</span>
        <span class="k">elif</span> <span class="n">Element</span><span class="o">.</span><span class="n">is_valid_symbol</span><span class="p">(</span><span class="n">sym</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">title</span><span class="p">()):</span>
            <span class="n">parsed_sym</span> <span class="o">=</span> <span class="n">sym</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">title</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">Element</span><span class="o">.</span><span class="n">is_valid_symbol</span><span class="p">(</span><span class="n">sym</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()):</span>
            <span class="n">parsed_sym</span> <span class="o">=</span> <span class="n">sym</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;w?[A-Z][a-z]*&quot;</span><span class="p">,</span> <span class="n">sym</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m</span><span class="p">:</span>
                <span class="n">parsed_sym</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">parsed_sym</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="p">(</span><span class="n">m_sp</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">\d*&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">parsed_sym</span><span class="p">),</span> <span class="n">sym</span><span class="p">)):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> parsed as </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sym</span><span class="p">,</span> <span class="n">parsed_sym</span><span class="p">)</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">parsed_sym</span>

    <span class="k">def</span> <span class="nf">_get_structure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">primitive</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate structure from part of the cif.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">get_num_implicit_hydrogens</span><span class="p">(</span><span class="n">sym</span><span class="p">):</span>
            <span class="n">num_h</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;Wat&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s2">&quot;wat&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s2">&quot;O-H&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span>
            <span class="k">return</span> <span class="n">num_h</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sym</span><span class="p">[:</span><span class="mi">3</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">lattice</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_lattice</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="c1"># if magCIF, get magnetic symmetry moments and magmoms</span>
        <span class="c1"># else standard CIF, and use empty magmom dict</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif_incommensurate&quot;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                <span class="s2">&quot;Incommensurate structures not currently supported.&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif&quot;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">symmetry_operations</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_magsymops</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="n">magmoms</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_magmoms</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">symmetry_operations</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_symops</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="n">magmoms</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="n">oxi_states</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_oxi_states</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="n">coord_to_species</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="n">coord_to_magmoms</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>

        <span class="k">def</span> <span class="nf">get_matching_coord</span><span class="p">(</span><span class="n">coord</span><span class="p">):</span>
            <span class="n">keys</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">coord_to_species</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
            <span class="n">coords</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">symmetry_operations</span><span class="p">:</span>
                <span class="n">c</span> <span class="o">=</span> <span class="n">op</span><span class="o">.</span><span class="n">operate</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>
                <span class="n">inds</span> <span class="o">=</span> <span class="n">find_in_coord_list_pbc</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span>
                                              <span class="n">atol</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_site_tolerance</span><span class="p">)</span>
                <span class="c1"># cant use if inds, because python is dumb and np.array([0]) evaluates</span>
                <span class="c1"># to False</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">inds</span><span class="p">):</span>
                    <span class="k">return</span> <span class="n">keys</span><span class="p">[</span><span class="n">inds</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">])):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="c1"># If site type symbol exists, use it. Otherwise, we use the</span>
                <span class="c1"># label.</span>
                <span class="n">symbol</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_symbol</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
                <span class="n">num_h</span> <span class="o">=</span> <span class="n">get_num_implicit_hydrogens</span><span class="p">(</span>
                    <span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="n">symbol</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_symbol</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
                <span class="n">num_h</span> <span class="o">=</span> <span class="n">get_num_implicit_hydrogens</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">symbol</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="n">oxi_states</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">o_s</span> <span class="o">=</span> <span class="n">oxi_states</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
                <span class="c1"># use _atom_site_type_symbol if possible for oxidation state</span>
                <span class="k">if</span> <span class="s2">&quot;_atom_site_type_symbol&quot;</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="n">oxi_symbol</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
                    <span class="n">o_s</span> <span class="o">=</span> <span class="n">oxi_states</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">oxi_symbol</span><span class="p">,</span> <span class="n">o_s</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">el</span> <span class="o">=</span> <span class="n">Specie</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span> <span class="n">o_s</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                    <span class="n">el</span> <span class="o">=</span> <span class="n">DummySpecie</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span> <span class="n">o_s</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">el</span> <span class="o">=</span> <span class="n">get_el_sp</span><span class="p">(</span><span class="n">symbol</span><span class="p">)</span>

            <span class="n">x</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_x&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
            <span class="n">y</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_y&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
            <span class="n">z</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_z&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
            <span class="n">magmom</span> <span class="o">=</span> <span class="n">magmoms</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">],</span>
                                 <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]))</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">occu</span> <span class="o">=</span> <span class="n">str2float</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;_atom_site_occupancy&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">):</span>
                <span class="n">occu</span> <span class="o">=</span> <span class="mi">1</span>

            <span class="k">if</span> <span class="n">occu</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">coord</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
                <span class="n">match</span> <span class="o">=</span> <span class="n">get_matching_coord</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>
                <span class="n">comp_d</span> <span class="o">=</span> <span class="p">{</span><span class="n">el</span><span class="p">:</span> <span class="n">occu</span><span class="p">}</span>
                <span class="k">if</span> <span class="n">num_h</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">comp_d</span><span class="p">[</span><span class="s2">&quot;H&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">num_h</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Structure has implicit hydrogens defined, &quot;</span>
                                         <span class="s2">&quot;parsed structure unlikely to be suitable for use &quot;</span>
                                         <span class="s2">&quot;in calculations unless hydrogens added.&quot;</span><span class="p">)</span>
                <span class="n">comp</span> <span class="o">=</span> <span class="n">Composition</span><span class="p">(</span><span class="n">comp_d</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">match</span><span class="p">:</span>
                    <span class="n">coord_to_species</span><span class="p">[</span><span class="n">coord</span><span class="p">]</span> <span class="o">=</span> <span class="n">comp</span>
                    <span class="n">coord_to_magmoms</span><span class="p">[</span><span class="n">coord</span><span class="p">]</span> <span class="o">=</span> <span class="n">magmom</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">coord_to_species</span><span class="p">[</span><span class="n">match</span><span class="p">]</span> <span class="o">+=</span> <span class="n">comp</span>
                    <span class="c1"># disordered magnetic not currently supported</span>
                    <span class="n">coord_to_magmoms</span><span class="p">[</span><span class="n">match</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="n">sum_occu</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coord_to_species</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">set</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="n">Element</span><span class="p">(</span><span class="s2">&quot;O&quot;</span><span class="p">),</span> <span class="n">Element</span><span class="p">(</span><span class="s2">&quot;H&quot;</span><span class="p">)}]</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">([</span><span class="n">o</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">sum_occu</span><span class="p">]):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Some occupancies (</span><span class="si">{}</span><span class="s2">) sum to &gt; 1! If they are within &quot;</span> \
                  <span class="s2">&quot;the occupancy_tolerance, they will be rescaled. &quot;</span> \
                  <span class="s2">&quot;The current occupancy_tolerance is set to: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sum_occu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_occupancy_tolerance</span><span class="p">)</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

        <span class="n">allspecies</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">allcoords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">allmagmoms</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">allhydrogens</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># check to see if magCIF file is disordered</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif&quot;</span><span class="p">]:</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">coord_to_magmoms</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="c1"># Proposed solution to this is to instead store magnetic</span>
                    <span class="c1"># moments as Specie &#39;spin&#39; property, instead of site</span>
                    <span class="c1"># property, but this introduces ambiguities for end user</span>
                    <span class="c1"># (such as unintended use of `spin` and Specie will have</span>
                    <span class="c1"># fictious oxidation state).</span>
                    <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                        <span class="s1">&#39;Disordered magnetic structures not currently supported.&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">coord_to_species</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">comp</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">groupby</span><span class="p">(</span>
                    <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">coord_to_species</span><span class="o">.</span><span class="n">items</span><span class="p">()),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
                    <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
                <span class="n">tmp_coords</span> <span class="o">=</span> <span class="p">[</span><span class="n">site</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">group</span><span class="p">]</span>
                <span class="n">tmp_magmom</span> <span class="o">=</span> <span class="p">[</span><span class="n">coord_to_magmoms</span><span class="p">[</span><span class="n">tmp_coord</span><span class="p">]</span> <span class="k">for</span> <span class="n">tmp_coord</span> <span class="ow">in</span>
                              <span class="n">tmp_coords</span><span class="p">]</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif&quot;</span><span class="p">]:</span>
                    <span class="n">coords</span><span class="p">,</span> <span class="n">magmoms</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_unique_coords</span><span class="p">(</span><span class="n">tmp_coords</span><span class="p">,</span>
                                                          <span class="n">magmoms_in</span><span class="o">=</span><span class="n">tmp_magmom</span><span class="p">,</span>
                                                          <span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">coords</span><span class="p">,</span> <span class="n">magmoms</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_unique_coords</span><span class="p">(</span><span class="n">tmp_coords</span><span class="p">)</span>

                <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">comp</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="n">Element</span><span class="p">(</span><span class="s2">&quot;O&quot;</span><span class="p">),</span> <span class="n">Element</span><span class="p">(</span><span class="s2">&quot;H&quot;</span><span class="p">)}:</span>
                    <span class="c1"># O with implicit hydrogens</span>
                    <span class="n">im_h</span> <span class="o">=</span> <span class="n">comp</span><span class="p">[</span><span class="s2">&quot;H&quot;</span><span class="p">]</span>
                    <span class="n">species</span> <span class="o">=</span> <span class="n">Composition</span><span class="p">({</span><span class="s2">&quot;O&quot;</span><span class="p">:</span> <span class="n">comp</span><span class="p">[</span><span class="s2">&quot;O&quot;</span><span class="p">]})</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">im_h</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="n">species</span> <span class="o">=</span> <span class="n">comp</span>

                <span class="n">allhydrogens</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span> <span class="o">*</span> <span class="p">[</span><span class="n">im_h</span><span class="p">])</span>
                <span class="n">allcoords</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span>
                <span class="n">allspecies</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span> <span class="o">*</span> <span class="p">[</span><span class="n">species</span><span class="p">])</span>
                <span class="n">allmagmoms</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">magmoms</span><span class="p">)</span>

            <span class="c1"># rescale occupancies if necessary</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">species</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">allspecies</span><span class="p">):</span>
                <span class="n">totaloccu</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">species</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
                <span class="k">if</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">totaloccu</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_occupancy_tolerance</span><span class="p">:</span>
                    <span class="n">allspecies</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">species</span> <span class="o">/</span> <span class="n">totaloccu</span>

        <span class="k">if</span> <span class="n">allspecies</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">allspecies</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">allcoords</span><span class="p">)</span> \
                <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">allspecies</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">allmagmoms</span><span class="p">):</span>
            <span class="n">site_properties</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">allhydrogens</span><span class="p">):</span>
                <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">allhydrogens</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">allcoords</span><span class="p">)</span>
                <span class="n">site_properties</span><span class="p">[</span><span class="s2">&quot;implicit_hydrogens&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">allhydrogens</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s2">&quot;magcif&quot;</span><span class="p">]:</span>
                <span class="n">site_properties</span><span class="p">[</span><span class="s2">&quot;magmom&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">allmagmoms</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">site_properties</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">site_properties</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="n">struct</span> <span class="o">=</span> <span class="n">Structure</span><span class="p">(</span><span class="n">lattice</span><span class="p">,</span> <span class="n">allspecies</span><span class="p">,</span> <span class="n">allcoords</span><span class="p">,</span>
                               <span class="n">site_properties</span><span class="o">=</span><span class="n">site_properties</span><span class="p">)</span>

            <span class="n">struct</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">get_sorted_structure</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">primitive</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_flags</span><span class="p">[</span><span class="s1">&#39;magcif&#39;</span><span class="p">]:</span>
                <span class="n">struct</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">get_primitive_structure</span><span class="p">(</span><span class="n">use_site_props</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">primitive</span><span class="p">:</span>
                <span class="n">struct</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">get_primitive_structure</span><span class="p">()</span>
                <span class="n">struct</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">get_reduced_structure</span><span class="p">()</span>

            <span class="k">return</span> <span class="n">struct</span>

<div class="viewcode-block" id="CifParser.get_structures"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.get_structures">[docs]</a>    <span class="k">def</span> <span class="nf">get_structures</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">primitive</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return list of structures in CIF file. primitive boolean sets whether a</span>
<span class="sd">        conventional cell structure or primitive cell structure is returned.</span>

<span class="sd">        Args:</span>
<span class="sd">            primitive (bool): Set to False to return conventional unit cells.</span>
<span class="sd">                Defaults to True. With magnetic CIF files, will return primitive</span>
<span class="sd">                magnetic cell which may be larger than nuclear primitive cell.</span>

<span class="sd">        Returns:</span>
<span class="sd">            List of Structures.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">structures</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">values</span><span class="p">()):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_structure</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">primitive</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">s</span><span class="p">:</span>
                    <span class="n">structures</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
                <span class="c1"># Warn the user (Errors should never pass silently)</span>
                <span class="c1"># A user reported a problem with cif files produced by Avogadro</span>
                <span class="c1"># in which the atomic coordinates are in Cartesian coords.</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;No structure parsed for </span><span class="si">%d</span><span class="s2"> structure in CIF. Section of CIF file below.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Error is </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Issues encountered while parsing CIF: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="p">))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">structures</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid cif file with no structures!&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">structures</span></div>

<div class="viewcode-block" id="CifParser.get_bibtex_string"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.get_bibtex_string">[docs]</a>    <span class="k">def</span> <span class="nf">get_bibtex_string</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get BibTeX reference from CIF file.</span>
<span class="sd">        :param data:</span>
<span class="sd">        :return: BibTeX string</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">pybtex.database</span> <span class="kn">import</span> <span class="n">BibliographyData</span><span class="p">,</span> <span class="n">Entry</span>
        <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Bibliographic data extraction requires pybtex.&quot;</span><span class="p">)</span>

        <span class="n">bibtex_keys</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;author&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_publ_author_name&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_author_name&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_publ_section_title&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_title&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;journal&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_name_full&#39;</span><span class="p">,</span> <span class="s1">&#39;_journal_name_abbrev&#39;</span><span class="p">,</span>
                                   <span class="s1">&#39;_citation_journal_full&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_journal_abbrev&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;volume&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_volume&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_journal_volume&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;year&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_year&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_year&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;number&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_number&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_number&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;page_first&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_page_first&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_page_first&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;page_last&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_page_last&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_page_last&#39;</span><span class="p">),</span>
                       <span class="s1">&#39;doi&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;_journal_DOI&#39;</span><span class="p">,</span> <span class="s1">&#39;_citation_DOI&#39;</span><span class="p">)}</span>

        <span class="n">entries</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># TODO: parse &#39;_publ_section_references&#39; when it exists?</span>
        <span class="c1"># TODO: CIF specification supports multiple citations.</span>

        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">values</span><span class="p">()):</span>

            <span class="c1"># convert to lower-case keys, some cif files inconsistent</span>
            <span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="o">.</span><span class="n">lower</span><span class="p">():</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>

            <span class="n">bibtex_entry</span> <span class="o">=</span> <span class="p">{}</span>

            <span class="k">for</span> <span class="n">field</span><span class="p">,</span> <span class="n">tags</span> <span class="ow">in</span> <span class="n">bibtex_keys</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">tag</span> <span class="ow">in</span> <span class="n">tags</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">tag</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">tag</span><span class="p">],</span> <span class="nb">list</span><span class="p">):</span>
                            <span class="n">bibtex_entry</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">tag</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">bibtex_entry</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">tag</span><span class="p">]</span>

            <span class="c1"># convert to bibtex author format (&#39;and&#39; delimited)</span>
            <span class="k">if</span> <span class="s1">&#39;author&#39;</span> <span class="ow">in</span> <span class="n">bibtex_entry</span><span class="p">:</span>
                <span class="c1"># separate out semicolon authors</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bibtex_entry</span><span class="p">[</span><span class="s2">&quot;author&quot;</span><span class="p">],</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="k">if</span> <span class="s2">&quot;;&quot;</span> <span class="ow">in</span> <span class="n">bibtex_entry</span><span class="p">[</span><span class="s2">&quot;author&quot;</span><span class="p">]:</span>
                        <span class="n">bibtex_entry</span><span class="p">[</span><span class="s2">&quot;author&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">bibtex_entry</span><span class="p">[</span><span class="s2">&quot;author&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">)</span>

                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bibtex_entry</span><span class="p">[</span><span class="s1">&#39;author&#39;</span><span class="p">],</span> <span class="nb">list</span><span class="p">):</span>
                    <span class="n">bibtex_entry</span><span class="p">[</span><span class="s1">&#39;author&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39; and &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">bibtex_entry</span><span class="p">[</span><span class="s1">&#39;author&#39;</span><span class="p">])</span>

            <span class="c1"># convert to bibtex page range format, use empty string if not specified</span>
            <span class="k">if</span> <span class="p">(</span><span class="s1">&#39;page_first&#39;</span> <span class="ow">in</span> <span class="n">bibtex_entry</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="s1">&#39;page_last&#39;</span> <span class="ow">in</span> <span class="n">bibtex_entry</span><span class="p">):</span>
                <span class="n">bibtex_entry</span><span class="p">[</span><span class="s1">&#39;pages&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}</span><span class="s1">--</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">bibtex_entry</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;page_first&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">),</span>
                                                          <span class="n">bibtex_entry</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;page_last&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
                <span class="n">bibtex_entry</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;page_first&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>  <span class="c1"># and remove page_first, page_list if present</span>
                <span class="n">bibtex_entry</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;page_last&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

            <span class="c1"># cite keys are given as cif-reference-idx in order they are found</span>
            <span class="n">entries</span><span class="p">[</span><span class="s1">&#39;cifref</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="p">)]</span> <span class="o">=</span> <span class="n">Entry</span><span class="p">(</span><span class="s1">&#39;article&#39;</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">bibtex_entry</span><span class="o">.</span><span class="n">items</span><span class="p">()))</span>

        <span class="k">return</span> <span class="n">BibliographyData</span><span class="p">(</span><span class="n">entries</span><span class="p">)</span><span class="o">.</span><span class="n">to_string</span><span class="p">(</span><span class="n">bib_format</span><span class="o">=</span><span class="s1">&#39;bibtex&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="CifParser.as_dict"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifParser.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cif</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">k2</span><span class="p">,</span> <span class="n">v2</span> <span class="ow">in</span> <span class="n">v</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="n">k2</span><span class="p">]</span> <span class="o">=</span> <span class="n">v2</span>
        <span class="k">return</span> <span class="n">d</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">has_errors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: Whether there are errors/warnings detected in CIF parsing.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">warnings</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span></div>


<div class="viewcode-block" id="CifWriter"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifWriter">[docs]</a><span class="k">class</span> <span class="nc">CifWriter</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper around CifFile to write CIF files from pymatgen structures.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">struct</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">write_magmoms</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">significant_figures</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">angle_tolerance</span><span class="o">=</span><span class="mf">5.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            struct (Structure): structure to write</span>
<span class="sd">            symprec (float): If not none, finds the symmetry of the structure</span>
<span class="sd">                and writes the cif with symmetry information. Passes symprec</span>
<span class="sd">                to the SpacegroupAnalyzer.</span>
<span class="sd">            write_magmoms (bool): If True, will write magCIF file. Incompatible</span>
<span class="sd">                with symprec</span>
<span class="sd">            significant_figures (int): Specifies precision for formatting of floats.</span>
<span class="sd">                Defaults to 8.</span>
<span class="sd">            angle_tolerance (float): Angle tolerance for symmetry finding. Passes</span>
<span class="sd">                angle_tolerance to the SpacegroupAnalyzer. Used only if symprec</span>
<span class="sd">                is not None.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">write_magmoms</span> <span class="ow">and</span> <span class="n">symprec</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s2">&quot;Magnetic symmetry cannot currently be detected by pymatgen,&quot;</span>
                <span class="s2">&quot;disabling symmetry detection.&quot;</span><span class="p">)</span>
            <span class="n">symprec</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="n">format_str</span> <span class="o">=</span> <span class="s2">&quot;{:.</span><span class="si">%d</span><span class="s2">f}&quot;</span> <span class="o">%</span> <span class="n">significant_figures</span>

        <span class="n">block</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="n">loops</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">spacegroup</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;P 1&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">symprec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sf</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">struct</span><span class="p">,</span> <span class="n">symprec</span><span class="p">,</span> <span class="n">angle_tolerance</span><span class="o">=</span><span class="n">angle_tolerance</span><span class="p">)</span>
            <span class="n">spacegroup</span> <span class="o">=</span> <span class="p">(</span><span class="n">sf</span><span class="o">.</span><span class="n">get_space_group_symbol</span><span class="p">(),</span>
                          <span class="n">sf</span><span class="o">.</span><span class="n">get_space_group_number</span><span class="p">())</span>
            <span class="c1"># Needs the refined struture when using symprec. This converts</span>
            <span class="c1"># primitive to conventional structures, the standard for CIF.</span>
            <span class="n">struct</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">get_refined_structure</span><span class="p">()</span>

        <span class="n">latt</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">lattice</span>
        <span class="n">comp</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">composition</span>
        <span class="n">no_oxi_comp</span> <span class="o">=</span> <span class="n">comp</span><span class="o">.</span><span class="n">element_composition</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_symmetry_space_group_name_H-M&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">spacegroup</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">cell_attr</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">]:</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_cell_length_&quot;</span> <span class="o">+</span> <span class="n">cell_attr</span><span class="p">]</span> <span class="o">=</span> <span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">latt</span><span class="p">,</span> <span class="n">cell_attr</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">cell_attr</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span> <span class="s1">&#39;beta&#39;</span><span class="p">,</span> <span class="s1">&#39;gamma&#39;</span><span class="p">]:</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_cell_angle_&quot;</span> <span class="o">+</span> <span class="n">cell_attr</span><span class="p">]</span> <span class="o">=</span> <span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">latt</span><span class="p">,</span> <span class="n">cell_attr</span><span class="p">))</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_symmetry_Int_Tables_number&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">spacegroup</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_chemical_formula_structural&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">no_oxi_comp</span><span class="o">.</span><span class="n">reduced_formula</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_chemical_formula_sum&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">no_oxi_comp</span><span class="o">.</span><span class="n">formula</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_cell_volume&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">latt</span><span class="o">.</span><span class="n">volume</span><span class="p">)</span>

        <span class="n">reduced_comp</span><span class="p">,</span> <span class="n">fu</span> <span class="o">=</span> <span class="n">no_oxi_comp</span><span class="o">.</span><span class="n">get_reduced_composition_and_factor</span><span class="p">()</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_cell_formula_units_Z&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fu</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">symprec</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_symmetry_equiv_pos_site_id&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;1&quot;</span><span class="p">]</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_symmetry_equiv_pos_as_xyz&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;x, y, z&quot;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sf</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">struct</span><span class="p">,</span> <span class="n">symprec</span><span class="p">)</span>

            <span class="n">symmops</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="n">sf</span><span class="o">.</span><span class="n">get_symmetry_operations</span><span class="p">():</span>
                <span class="n">v</span> <span class="o">=</span> <span class="n">op</span><span class="o">.</span><span class="n">translation_vector</span>
                <span class="n">symmops</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SymmOp</span><span class="o">.</span><span class="n">from_rotation_and_translation</span><span class="p">(</span>
                    <span class="n">op</span><span class="o">.</span><span class="n">rotation_matrix</span><span class="p">,</span> <span class="n">v</span><span class="p">))</span>

            <span class="n">ops</span> <span class="o">=</span> <span class="p">[</span><span class="n">op</span><span class="o">.</span><span class="n">as_xyz_string</span><span class="p">()</span> <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="n">symmops</span><span class="p">]</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_symmetry_equiv_pos_site_id&quot;</span><span class="p">]</span> <span class="o">=</span> \
                <span class="p">[</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">ops</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_symmetry_equiv_pos_as_xyz&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ops</span>

        <span class="n">loops</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s2">&quot;_symmetry_equiv_pos_site_id&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_symmetry_equiv_pos_as_xyz&quot;</span><span class="p">])</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">symbol_to_oxinum</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([</span>
                <span class="p">(</span><span class="n">el</span><span class="o">.</span><span class="fm">__str__</span><span class="p">(),</span>
                 <span class="nb">float</span><span class="p">(</span><span class="n">el</span><span class="o">.</span><span class="n">oxi_state</span><span class="p">))</span>
                <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">comp</span><span class="o">.</span><span class="n">elements</span><span class="p">)])</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_type_symbol&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">symbol_to_oxinum</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_type_oxidation_number&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">symbol_to_oxinum</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
            <span class="n">loops</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s2">&quot;_atom_type_symbol&quot;</span><span class="p">,</span> <span class="s2">&quot;_atom_type_oxidation_number&quot;</span><span class="p">])</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="ne">AttributeError</span><span class="p">):</span>
            <span class="n">symbol_to_oxinum</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="n">el</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span>
                                            <span class="nb">sorted</span><span class="p">(</span><span class="n">comp</span><span class="o">.</span><span class="n">elements</span><span class="p">)])</span>

        <span class="n">atom_site_type_symbol</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_symmetry_multiplicity</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_fract_x</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_fract_y</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_fract_z</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_label</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_occupancy</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_moment_label</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_moment_crystalaxis_x</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_moment_crystalaxis_y</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">atom_site_moment_crystalaxis_z</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">symprec</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">struct</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">sp</span><span class="p">,</span> <span class="n">occu</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
                    <span class="n">atom_site_type_symbol</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span>
                    <span class="n">atom_site_symmetry_multiplicity</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;1&quot;</span><span class="p">)</span>
                    <span class="n">atom_site_fract_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">a</span><span class="p">))</span>
                    <span class="n">atom_site_fract_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">b</span><span class="p">))</span>
                    <span class="n">atom_site_fract_z</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">c</span><span class="p">))</span>
                    <span class="n">atom_site_label</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="n">count</span><span class="p">))</span>
                    <span class="n">atom_site_occupancy</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">occu</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span>

                    <span class="n">magmom</span> <span class="o">=</span> <span class="n">Magmom</span><span class="p">(</span>
                        <span class="n">site</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;magmom&#39;</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">sp</span><span class="p">,</span> <span class="s1">&#39;spin&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)))</span>
                    <span class="k">if</span> <span class="n">write_magmoms</span> <span class="ow">and</span> <span class="nb">abs</span><span class="p">(</span><span class="n">magmom</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">moment</span> <span class="o">=</span> <span class="n">Magmom</span><span class="o">.</span><span class="n">get_moment_relative_to_crystal_axes</span><span class="p">(</span>
                            <span class="n">magmom</span><span class="p">,</span> <span class="n">latt</span><span class="p">)</span>
                        <span class="n">atom_site_moment_label</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="s2">&quot;</span><span class="si">{}{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="n">count</span><span class="p">))</span>
                        <span class="n">atom_site_moment_crystalaxis_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">moment</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
                        <span class="n">atom_site_moment_crystalaxis_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">moment</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
                        <span class="n">atom_site_moment_crystalaxis_z</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">moment</span><span class="p">[</span><span class="mi">2</span><span class="p">]))</span>

                    <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># The following just presents a deterministic ordering.</span>
            <span class="n">unique_sites</span> <span class="o">=</span> <span class="p">[</span>
                <span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">sites</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="nb">tuple</span><span class="p">([</span><span class="nb">abs</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span>
                                                    <span class="n">s</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">]))[</span><span class="mi">0</span><span class="p">],</span>
                 <span class="nb">len</span><span class="p">(</span><span class="n">sites</span><span class="p">))</span>
                <span class="k">for</span> <span class="n">sites</span> <span class="ow">in</span> <span class="n">sf</span><span class="o">.</span><span class="n">get_symmetrized_structure</span><span class="p">()</span><span class="o">.</span><span class="n">equivalent_sites</span>
            <span class="p">]</span>
            <span class="k">for</span> <span class="n">site</span><span class="p">,</span> <span class="n">mult</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span>
                    <span class="n">unique_sites</span><span class="p">,</span>
                    <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">average_electroneg</span><span class="p">,</span>
                                   <span class="o">-</span><span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">b</span><span class="p">,</span> <span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">c</span><span class="p">)):</span>
                <span class="k">for</span> <span class="n">sp</span><span class="p">,</span> <span class="n">occu</span> <span class="ow">in</span> <span class="n">site</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="n">atom_site_type_symbol</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span>
                    <span class="n">atom_site_symmetry_multiplicity</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">mult</span><span class="p">)</span>
                    <span class="n">atom_site_fract_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">a</span><span class="p">))</span>
                    <span class="n">atom_site_fract_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">b</span><span class="p">))</span>
                    <span class="n">atom_site_fract_z</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">format_str</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">c</span><span class="p">))</span>
                    <span class="n">atom_site_label</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="n">count</span><span class="p">))</span>
                    <span class="n">atom_site_occupancy</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">occu</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span>
                    <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_type_symbol</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_label&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_label</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_symmetry_multiplicity&quot;</span><span class="p">]</span> <span class="o">=</span> \
            <span class="n">atom_site_symmetry_multiplicity</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_x&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_fract_x</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_y&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_fract_y</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_fract_z&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_fract_z</span>
        <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_occupancy&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_occupancy</span>
        <span class="n">loops</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s2">&quot;_atom_site_type_symbol&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_atom_site_label&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_atom_site_symmetry_multiplicity&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_atom_site_fract_x&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_atom_site_fract_y&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_atom_site_fract_z&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;_atom_site_occupancy&quot;</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">write_magmoms</span><span class="p">:</span>
            <span class="n">block</span><span class="p">[</span><span class="s2">&quot;_atom_site_moment_label&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_moment_label</span>
            <span class="n">block</span><span class="p">[</span>
                <span class="s2">&quot;_atom_site_moment_crystalaxis_x&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_moment_crystalaxis_x</span>
            <span class="n">block</span><span class="p">[</span>
                <span class="s2">&quot;_atom_site_moment_crystalaxis_y&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_moment_crystalaxis_y</span>
            <span class="n">block</span><span class="p">[</span>
                <span class="s2">&quot;_atom_site_moment_crystalaxis_z&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">atom_site_moment_crystalaxis_z</span>
            <span class="n">loops</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s2">&quot;_atom_site_moment_label&quot;</span><span class="p">,</span>
                          <span class="s2">&quot;_atom_site_moment_crystalaxis_x&quot;</span><span class="p">,</span>
                          <span class="s2">&quot;_atom_site_moment_crystalaxis_y&quot;</span><span class="p">,</span>
                          <span class="s2">&quot;_atom_site_moment_crystalaxis_z&quot;</span><span class="p">])</span>
        <span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="n">d</span><span class="p">[</span><span class="n">comp</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">]</span> <span class="o">=</span> <span class="n">CifBlock</span><span class="p">(</span><span class="n">block</span><span class="p">,</span> <span class="n">loops</span><span class="p">,</span> <span class="n">comp</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cf</span> <span class="o">=</span> <span class="n">CifFile</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">ciffile</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns: CifFile associated with the CifWriter.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cf</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the cif as a string.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cf</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span>

<div class="viewcode-block" id="CifWriter.write_file"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.CifWriter.write_file">[docs]</a>    <span class="k">def</span> <span class="nf">write_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Write the cif file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">zopen</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">&quot;wt&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span></div></div>


<div class="viewcode-block" id="str2float"><a class="viewcode-back" href="../../../pymatgen.io.cif.html#pymatgen.io.cif.str2float">[docs]</a><span class="k">def</span> <span class="nf">str2float</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Remove uncertainty brackets from strings and return the float.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="c1"># Note that the ending ) is sometimes missing. That is why the code has</span>
        <span class="c1"># been modified to treat it as optional. Same logic applies to lists.</span>
        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\(.+\)*&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">))</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\(.+\)*&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
    <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">text</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s2">&quot;.&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">raise</span> <span class="n">ex</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.cif</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>